package emailBox

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/emailBox"
	emailBoxReq "github.com/flipped-aurora/gin-vue-admin/server/model/emailBox/request"
)

type EmailINBOXService struct {
}

// CreateEmailINBOX 创建EmailINBOX记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailINBOXService *EmailINBOXService) CreateEmailINBOX(emailINBOX emailBox.EmailINBOX) (err error) {
	err = global.GVA_DB.Create(&emailINBOX).Error
	return err
}

// DeleteEmailINBOX 删除EmailINBOX记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailINBOXService *EmailINBOXService) DeleteEmailINBOX(emailINBOX emailBox.EmailINBOX) (err error) {
	err = global.GVA_DB.Delete(&emailINBOX).Error
	return err
}

// DeleteEmailINBOXByIds 批量删除EmailINBOX记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailINBOXService *EmailINBOXService) DeleteEmailINBOXByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]emailBox.EmailINBOX{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateEmailINBOX 更新EmailINBOX记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailINBOXService *EmailINBOXService) UpdateEmailINBOX(emailINBOX emailBox.EmailINBOX) (err error) {
	err = global.GVA_DB.Save(&emailINBOX).Error
	return err
}

// GetEmailINBOX 根据id获取EmailINBOX记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailINBOXService *EmailINBOXService) GetEmailINBOX(id uint) (emailINBOX emailBox.EmailINBOX, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&emailINBOX).Error
	return
}

// GetEmailINBOXInfoList 分页获取EmailINBOX记录
// Author [piexlmax](https://github.com/piexlmax)
func (emailINBOXService *EmailINBOXService) GetEmailINBOXInfoList(info emailBoxReq.EmailINBOXSearch) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&emailBox.EmailINBOX{})
	var emailINBOXs []emailBox.EmailINBOX
	fmt.Println(info)
	if len(info.Auth) != 0 {
		db = db.Where("auth=?", info.Auth)
	}
	if info.Status != nil {
		db = db.Where("status=?", info.Status)
	} else {
		db = db.Where("status not in (3,4)")
	}
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Find(&emailINBOXs).Error
	return emailINBOXs, total, err
}
